package mvp.com.bbc.rcjavaretrofitdaggermvp.mvp.fragment;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import mvp.com.bbc.rcjavaretrofitdaggermvp.R;
import mvp.com.bbc.rcjavaretrofitdaggermvp.app.contact.ArtListContact;
import mvp.com.bbc.rcjavaretrofitdaggermvp.baseAF.BaseFragment;
import mvp.com.bbc.rcjavaretrofitdaggermvp.bean.ThemeListBean;
import mvp.com.bbc.rcjavaretrofitdaggermvp.di.component.DaggerThemNameComponent;
import mvp.com.bbc.rcjavaretrofitdaggermvp.di.modules.ThemNameModules;
import mvp.com.bbc.rcjavaretrofitdaggermvp.mvp.adapter.BaseFrgamenAdapter;
import mvp.com.bbc.rcjavaretrofitdaggermvp.presenter.ThemNamePresenter;
import tech.michaelx.loadinglibrary.LoadingLayout;

/**
 * Created by ${cbf} on 2017/12/2.
 */

public class ArticeListFragment extends BaseFragment implements ArtListContact.ThemNameView {
    @BindView(R.id.tablayout)
    TabLayout tablayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.loadinglayout)
    LoadingLayout loadinglayout;
    private ThemNamePresenter presenter;
    private String type = "1";

    public static Fragment newInstance(String type) {
        ArticeListFragment fragment = new ArticeListFragment();
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int setContentLayout() {
        return R.layout.fragment_artice_hotmom;
    }

    @Override
    protected void findViews() {

    }

    @Override
    protected void init() {
        Bundle bundle = getArguments();
        type = bundle.getString("type");
        if (type == null) {
            type = "1";
        }
        presenter = DaggerThemNameComponent.builder()
                .themNameModules(new ThemNameModules(this))
                .build()
                .getPresenter();
        presenter.themNamePresneter();
    }

    @Override
    protected void setListeners() {

    }

    @Override
    public void onClick(View view) {

    }


    @Override
    public void themNameView(ThemeListBean listBean) {
        List<ThemeListBean.TeBean> te = listBean.getTe();
        if (te == null && te.size() < 0) {
            loadinglayout.showEmpty();
        } else {
            for (int i = 0; i < te.size(); i++) {
                List<Fragment> fragments = new ArrayList<>();
                String[] title = new String[]{te.get(0).getName(), te.get(1).getName(), te.get(2).getName()};
                fragments.add(HotMomFragment.newInstance("1"));
                fragments.add(HotMomFragment.newInstance("2"));
                fragments.add(HotMomFragment.newInstance("3"));
                if (type.equals("1")) {
                    BaseFrgamenAdapter adapter = new BaseFrgamenAdapter(getChildFragmentManager(), fragments, title);
                    viewPager.setAdapter(adapter);
                    tablayout.setupWithViewPager(viewPager);
                }
            }
            loadinglayout.loadComplete();
        }
    }

    @Override
    public void onEvent(String action) {
        super.onEvent(action);
        if (action.equals("网络请求失败")) {
            loadinglayout.loadFailure();
        }
        if (action.equals("网络请求成功")) {
            loadinglayout.loadComplete();
        }
    }
}
